<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/View.php');

Class CalendarView extends BaseView
{
	function CalendarView()
	{
		parent::BaseView();
	}
	
	function PrintUpcomming(&$calendar, $error)
	{
		if ($error[0])
		{
			$this->PrintError($error);
		}
		else
		{
			$calendar = preg_replace("|\s*xmlns=['\"][^'\"]*['\"]|", "", $calendar);
			//$calendar = utf8_encode($calendar);
			$dom = domxml_open_mem($calendar);
			$dom = new DomDocument($calendar);
			$xpath = $this->GetXpathContext($dom);
			
			$nodes = $xpath->xpath_eval('//entry');
			$items = $nodes->nodeset;
			$events = array();

			foreach ($items as $key=>$item)
			{
				$title = htmlspecialchars($this->GetElementValue($item, 'title'));
				$content = htmlspecialchars($this->GetElementValue($item, 'content'));
				preg_match('/Wanneer:\s*(?<date>.+)\s*\n/', $content, $c);
				preg_match("/(?<weekdag>\w{2})\s+(?<dag>\d{1,2})\.\s+(?<maand>\w{3})\s+(?<jaar>\d{4})\s+(?<van>[\d|:]{4,5})\s+-\s+(?<tot>[\d|:]{4,5})/", $c['date'], $time);
//print('<pre>'.var_export($time, true).'</pre>');exit;
				preg_match('/Waar:\s*(?<loc>.+)\s*\n/', $content, $c);
				$plaats = $c['loc'];
				$l = $this->GetXpath($item, '/link[@rel="alternate"]');
				$link = $l->nodeset[0]->get_attribute('href');
				$van = split(':', $time['van']);
				
				$datetime = mktime($van[0], $van[1], 0, $this->MonthNumber($time['maand']), $time['dag'], $time['jaar']);
				$events[$datetime] = array( 
					'datetime' => $datetime, 
					'title' => $title, 
					'content' => $content, 
					'time' => $time, 
					'location' => $plaats, 
					'link' => $link);
			}

			$this->output = '<ul class="calendar upcoming">';
			asort($events);
			foreach ($events as $key=>$event)
			{
				$this->output .= '<li>';
				$this->output .= '<a href="'.$event['link'].'" class="title">' . $event['title'] . '</a>';
				$this->output .= '<p class="time">';
				//$this->output .= date('D j F Y', $event['datetime']).'<br />';
				$this->output .= $event['time']['weekdag'].' '.$event['time']['dag'].' '.$event['time']['maand'].' '.$event['time']['jaar'];
				$this->output .= ': ';
				$this->output .= $event['time']['van'];//.' - '.$event['time']['tot'];
				$this->output .= '</p>';
				$this->output .= '<p class="location">';
				$this->output .= $event['location'];
				$this->output .= '</p>';
				$this->output .= '</li>';
			}

			$this->output .= '</ul>';
		}
	}
	
	function PrintError($error)
	{
		$this->output = '<span class="error">Er is een fout opgetreden in de blog: ['.$error[0].'] '.$error[1].'</span>';
	}
	
	/* Date/Time Functions */
	function MonthNumber($month)
	{
		switch($month)
		{
			case 'jan': return 1;
			case 'feb': return 2;
			case 'mrt': return 3;
			case 'apr': return 4;
			case 'mei': return 5;
			case 'jun': return 6;
			case 'jul': return 7;
			case 'aug': return 8;
			case 'sep': return 9;
			case 'okt': return 10;
			case 'nov': return 11;
			case 'dec': return 12;
		}
	}
	
	/* XML Functions */
	function GetXpathContext($dom)
	{
		$xpath = xpath_new_context($dom);
		xpath_register_ns($xpath, '', 'http://www.w3.org/2005/Atom');
		xpath_register_ns($xpath, 'openSearch', 'http://a9.com/-/spec/opensearchrss/1.0/');
		
		return $xpath;
	}
	
	function GetElementValue($node, $elementName)
	{
		$elements = $node->get_elements_by_tagname($elementName);
		$element = $elements[0];
		$value = $element->get_content();
		
		return $value;
	}
	
	function GetXpath($node, $xpath)
	{
		$xpathContext = xpath_new_context($node);
		$elements = $xpathContext->xpath_eval($xpath);

		return $elements;
	}
	
	function GetNodeValue($xpathContext, $xpath)
	{
		$nodes = $xpathContext->xpath_eval($xpath);
		$node = $nodes->nodeset[0]->first_child();
		$value = $node->node_value();
		
		return $value;
	}
	
	function GetNodeContent($dom, $xpathContext, $xpath)
	{
		$nodes = $xpathContext->xpath_eval($xpath);
		$node = $nodes->nodeset[0]->first_child();
		$value = $dom->dump_node($node);
		
		return $value;
	}
}
?>